<template>
    <div id='electronic' class="set">
      <Row type="flex" align="middle" justify="center" class="set_ct">
        <Col>
          <Form ref="formDynamic" :model="formDynamic" :rules="ruleValidate" :label-width="180">
            <FormItem label="是否采用全电子币经营模式：" prop='radio'>
              <RadioGroup v-model="formDynamic.radio">
                <Radio label="1" style="margin-left:35px">是</Radio>
                <Radio label="0" style="margin-left:35px">否</Radio>
              </RadioGroup>
            </FormItem>
            <FormItem>
              <Button type="primary"  @click="handleSubmit" class="button">确定</Button>
              <Button @click="cancel" class="button btn1">取消</Button>
            </FormItem>
          </Form>
        </Col>
      </Row>
    </div>
  </template>

<script>
import './index.less'
import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (vm.routerId !== to.query.id) {
        vm.shopId = to.query.id
        vm.paramter.shopId = to.query.id
        vm.getData()
      }
    })
  },
  data () {
    return {
      formDynamic: {
        radio: ''
      },
      ruleValidate: {
        radio: { required: true, message: '请选择是否采用全电子币经营模式', trigger: 'blur' }
      },
      paramter: {
        'merchantId': this.$store.state.user.merchantId,
        'msType': '1',
        'shopId': this.$route.query.id
      }
    }
  },
  mounted () {
    this.shopId = this.$route.query.id
    this.getData()
  },
  methods: {
    getData () {
      let obj = Object.assign({}, this.paramter, { 'dictKeys': 'electronicCoin' })
      getDicQuery(obj).then((res) => {
        if (res.data.status === 200) {
          this.formDynamic.radio = res.data.data[0].electronicCoin + ''
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
      if (!this.hasPermission('moms:setting:virtualcoin')) {
        this.$router.replace({ name: 'error_401' })
      }
    },
    handleSubmit () {
      this.$refs.formDynamic.validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.paramter, { 'electronicCoin': this.formDynamic.radio })
          setDicSave(data).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功!')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    cancel () {
      this.closeModal()
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      if (this.$store.state.app.tagNavList[this.$store.state.app.tagNavList.length - 1].name !== 'store_setting') {
        this.$nextTick(() => {
          this.$router.push({
            name: 'store_setting', params: { id: this.shopId }
          })
        })
      }
    }
  }
}
</script>
